//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.Provenance.Controls
{
    using Microsoft.Research.Provenance.Controls.Presenter;
    using Microsoft.Research.ProvenanceInterfaces;
    using Microsoft.Practices.Unity;

    public class ProvenanceModule : IProvenanceModule
    {
        #region Private fields
        /// <summary>
        /// Instance of the Unity container
        /// </summary>
        private IUnityContainer container;

        /// <summary>
        /// Instance of the Monitor service
        /// </summary>
        private IProvenance provenance;

        /// <summary>
        /// Instance of the Version presenter.
        /// </summary>
        private VersionHistoryPresenter vHPresenter;

        /// <summary>
        /// Instance of the workflow data product presenter.
        /// </summary>
        private WorkflowDataProductPresenter workFlowDPPresenter;

        /// <summary>
        /// Instance of the workflow inputs presenter.
        /// </summary>
        private WorkflowInputPresenter workFlowInput;

        /// <summary>
        /// Instance of the workflow outputs presenter.
        /// </summary>
        private WorkflowOutputPresenter workFlowOutputs;

        /// <summary>
        /// Instance of the workflow summary presenter.
        /// </summary>
        private WorkflowSummaryPresenter workFlowSummary;

        /// <summary>
        /// Instance of the detailed provenance presenter.
        /// </summary>
        private DetailedProvenancePresenter activityDetails;

        /// <summary>
        /// Instance of the region manager
        /// </summary>
        private IProvenanceRegionManager regionManager;
        #endregion

        /// <summary>
        /// Initializes a new instance of the MonitorModule class
        /// </summary>
        /// <param name="container">instance of the Unity container</param>
        /// <param name="regionManager">instance of the region manager</param>
        public ProvenanceModule(IUnityContainer container, IProvenanceRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }

        #region IProvenanceModule Members
        /// <summary>
        /// Initialize the module. Instantiate the presenters.
        /// </summary>
        public void Initialize()
        {
            this.RegisterViewsAndServices();

            this.vHPresenter = this.container.Resolve<VersionHistoryPresenter>();
            this.workFlowDPPresenter = this.container.Resolve<WorkflowDataProductPresenter>();
            this.workFlowInput = this.container.Resolve<WorkflowInputPresenter>();
            this.workFlowOutputs = this.container.Resolve<WorkflowOutputPresenter>();
            this.workFlowSummary = this.container.Resolve<WorkflowSummaryPresenter>();
            this.activityDetails = this.container.Resolve<DetailedProvenancePresenter>();

            this.regionManager.Add(this.workFlowSummary.View, "WorkFlowSummaryGrid");
            this.regionManager.Add(this.workFlowDPPresenter.View, "WorkFlowDataProductGrid");
            this.regionManager.Add(this.workFlowInput.View, "WorkFlowParametersGrid");
            this.regionManager.Add(this.workFlowOutputs.View, "WorkFlowOutputsGrid");
            this.regionManager.Add(this.vHPresenter.View, "VersionHistoryGrid");
            this.regionManager.Add(this.activityDetails.View, "ActivityDetailedProvenance");

            this.workFlowSummary.IsExpanded = true;
            this.workFlowInput.IsExpanded = true;
            this.workFlowOutputs.IsExpanded = true;
        }

        #endregion

        /// <summary>
        /// Registers the views and services to the Unity container
        /// </summary>
        private void RegisterViewsAndServices()
        {
            this.provenance = this.container.Resolve<Provenance>();
            this.container.RegisterInstance<IProvenance>(this.provenance, new ExternallyControlledLifetimeManager());
        }
    }
}
